package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author shkstart
 * @create 2023-01-07-22:19
 * APP文章上下架MQ监听器
 */
@Component
public class ArticleDownOrUpListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    private void msg(ConsumerRecord<String,String> consumerRecord){

        if (consumerRecord != null){
            String json = consumerRecord.value();
            Map map = JSON.parseObject(json, Map.class);
            Long articleId = Long.valueOf(String.valueOf(map.get("articleId")));
            Integer enable = Integer.valueOf(String.valueOf(map.get("enable")));

            Boolean isDown = true;  //1已下架
            if (enable == 1){
                isDown = false;// 0 未下架（已下架 ）
            }
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId,articleId)
                    .set(ApArticleConfig::getIsDown, isDown)
            );
        }
    }
}
